package com.huluobo.a2302mdmdemo

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.graphics.BitmapFactory
import com.amap.api.location.AMapLocationClient
import com.huluobo.lib_base.BaseApp
import com.hyphenate.EMMessageListener
import com.hyphenate.chat.EMClient
import com.hyphenate.chat.EMMessage
import com.hyphenate.chat.EMOptions
import com.hyphenate.chat.EMTextMessageBody
import com.tencent.mmkv.MMKV
import dagger.hilt.android.HiltAndroidApp


/**
 *  Created by LC on 2025/7/22.
 *  moroks#2211mouthdemo
 */
@HiltAndroidApp
class MDMApp : BaseApp() {
    private val emMessageListener = EMMessageListener {
        //弹出消息通知
        showNotification(it)
    }

    private fun showNotification(messages: List<EMMessage>) {
        val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager

        //获取通知的频道
        val channel = NotificationChannel("channel", "1", NotificationManager.IMPORTANCE_DEFAULT)
        notificationManager.createNotificationChannel(channel)
        messages.forEach {
            var contentText = "文本"
            if (it.type == EMMessage.Type.TXT) {
                contentText = (it.body as EMTextMessageBody).message
            } else if (it.type == EMMessage.Type.IMAGE) {
                contentText = "[图片]"
            } else if (it.type == EMMessage.Type.FILE) {
                contentText = "[文件]"
            }

            val notification = Notification.Builder(this)
                .setContentTitle("收到新消息")
                .setSmallIcon(R.drawable.icon)
                .setLargeIcon(BitmapFactory.decodeResource(resources, R.drawable.apps))
                .setContentText(contentText)
                .setAutoCancel(true)
                .setChannelId("channel")
                .build()

            notificationManager.notify(1, notification)
        }

    }

    override fun onCreate() {
        super.onCreate()
        //MMKV初始化,替代SP
        MMKV.initialize(this)
        //高德地图初始化需要调用的隐私合规
        AMapLocationClient.updatePrivacyShow(this, true, true)
        AMapLocationClient.updatePrivacyAgree(this, true)

        val options = EMOptions()
        options.appKey = "moroks#2211mouthdemo"
        EMClient.getInstance().init(context, options)
        EMClient.getInstance().chatManager().addMessageListener(emMessageListener)
    }
}